import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:mfkb/config/Config.dart';
import 'package:mfkb/pages/modules/CustomRoute.dart';
import 'package:mfkb/pages/modules/Nothing.dart';
import 'package:mfkb/pages/school/market/SchoolMarketDetail.dart';
import 'package:mfkb/pages/web/WebViewByUrl.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/MarketService.dart';
import 'package:toast/toast.dart';

class SchoolMarket extends StatefulWidget {
  @override
  _SchoolMarketState createState() => _SchoolMarketState();
}

class _SchoolMarketState extends State<SchoolMarket> {
  List<dynamic> list = null ;
  List<dynamic> markets = null ;

  @override
  void initState() {
    super.initState();
    MarketService().init().then((value){
      if(value["code"] == 200){
        setState(() {
          list = value["data"]["banners"] ;
          markets = value["data"]["markets"] ;
        });
      }else{
        Toast.show(value["msg"], context) ;
      }
    }) ;
  }
  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark ,
      child: Scaffold(
        body: Container(
          width: ScreenUtil().setWidth(375),
          height: ScreenUtil().setHeight(812),
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [Color.fromARGB(255, 255, 255, 255) , Color.fromARGB(255, 246, 245, 248)] ,
              begin: Alignment.topCenter ,
              end: Alignment.bottomCenter
            )
          ),
          child: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).padding.top ,
              ) ,
              Container(
                height: 45,
                child: Row(
                  children: <Widget>[
                    Container(
                      width: 17,
                      height: 17,
                      margin: EdgeInsets.only(left:ScreenUtil().setWidth(19)),
                      child: GestureDetector(
                        child: Container(
                          width: 17,
                          height: 17,
                          child: Image.asset("assets/images/back.png" , fit: BoxFit.fill,),
                        ),
                        onTap: (){
                          Navigator.of(context).pop() ;
                        },
                      ),
                    ) ,
                    Expanded(
                      child: Container(
                        child: Center(
                          child: Text("校园商城" , style: TextStyle(
                            color: Color.fromARGB(255, 0, 0, 0) ,
                            fontSize: ScreenUtil().setSp(18) ,
                            fontWeight: FontWeight.bold 
                          ),),
                        ),
                      ),
                    ) ,
                    Container(
                      height: 17,
                      width: 36,
                    )
                  ],
                ),
              ) ,
              Container(
                width:ScreenUtil().setWidth(345) ,
                height:ScreenUtil().setHeight(110) ,
                margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , top:ScreenUtil().setHeight(8) , right:ScreenUtil().setWidth(15)),
                child: Swiper(
                  itemCount: list == null ? 1 : list.length ,
                  autoplay: true,
                  scale: 0.9,
                  itemBuilder: (BuildContext context,int index){
                    // 使用缓存图片类
                    return ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(15)),
                      child: GestureDetector(
                        child: CachedNetworkImage(
                          imageUrl:list == null ? "" : Config().getImgHost()+list[index]["img"] ,
                          fit: BoxFit.fill ,
                          placeholder: (context , url){
                            return (
                              Container(
                                color: Color.fromARGB(255, 246, 245, 248),
                                child: Center(
                                  child: Image.asset("assets/images/imageloading.png"),
                                ),
                              )
                            ) ;
                          } ,
                        ),
                        onTap: (){
                          if(list[index]["type"] == 1){
                            Navigator.of(context).push(CustomRoute(WebViewByUrl(url: list[index]["url"]))) ;
                          }
                        },
                      ) 
                    ) ;
                  },
                )
              ) ,
              Expanded(
                child:Container(
                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(12)),
                  child:ScrollConfiguration(
                    behavior: MyBehavior() ,
                    child: ListView(
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(15)),
                          child: markets == null ? Column(
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(150),
                                height: ScreenUtil().setWidth(150),
                                margin: EdgeInsets.only(top:ScreenUtil().setHeight(100)),
                                child: Image.asset("assets/images/nothing.png"),
                              ) ,
                              Text(
                                "暂无数据" , style: TextStyle(
                                  fontSize: ScreenUtil().setSp(16) ,
                                  fontWeight: FontWeight.bold ,
                                  color: Color.fromARGB(255, 43, 50, 62) ,
                                  decoration: TextDecoration.none
                                ),
                              ) ,
                              Container(
                                margin: EdgeInsets.only(top:ScreenUtil().setHeight(9)),
                                child: Text(
                                  "未找到您要的数据" , style: TextStyle(
                                    fontSize: ScreenUtil().setSp(12) ,
                                    color: Color.fromARGB(255, 86, 102, 122) ,
                                    decoration: TextDecoration.none
                                  ),
                                ),
                              )
                            ],
                          ) : Wrap(
                            spacing: ScreenUtil().setWidth(8),
                            runSpacing: ScreenUtil().setHeight(8),
                            children: List.generate(markets.length , (index){
                              return GestureDetector(
                                child: Container(
                                  width: ScreenUtil().setWidth(168),
                                  height: ScreenUtil().setHeight(90),
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      color:Color.fromARGB(255, 236, 237, 241) ,
                                      width: 1
                                    ) ,
                                    color: Colors.white ,
                                    borderRadius: BorderRadius.all(Radius.circular(8))
                                  ),
                                  child: Stack(
                                    children: <Widget>[
                                      Positioned(
                                        right: 0 ,
                                        top: 0 ,
                                        child: Container(
                                          width: ScreenUtil().setWidth(90),
                                          height: ScreenUtil().setHeight(90),
                                          child: CachedNetworkImage(
                                            imageUrl: Config().getImgHost()+markets[index]["img"],
                                            placeholder: (context , url){
                                              return (
                                                Container(
                                                  color: Color.fromARGB(255, 246, 245, 248),
                                                  child: Center(
                                                    child: Image.asset("assets/images/imageloading.png"),
                                                  ),
                                                )
                                              ) ;
                                            } ,
                                          ),
                                        ),
                                      ) ,
                                      Positioned(
                                        top:ScreenUtil().setHeight(15) ,
                                        left: ScreenUtil().setWidth(17),
                                        child: Container(
                                          width: ScreenUtil().setWidth(80),
                                          height: ScreenUtil().setHeight(25),
                                          child: Text(
                                            markets[index]["name"] , style: TextStyle(
                                              fontSize: ScreenUtil().setSp(18) ,
                                              height: ScreenUtil().setHeight(25) / ScreenUtil().setSp(18) ,
                                              fontWeight: FontWeight.bold ,
                                              color: Color.fromARGB(255, 19, 28, 53)
                                            ),
                                          ),
                                        ),
                                      ) ,
                                      Positioned(
                                        top:ScreenUtil().setHeight(45) ,
                                        left: ScreenUtil().setWidth(17),
                                        child: Container(
                                          height: ScreenUtil().setHeight(17),
                                          child: Text(markets[index]["msg"] , style: TextStyle(
                                            color: Color.fromARGB(255, 78, 86, 108) ,
                                            fontSize: ScreenUtil().setSp(12) ,
                                            height: ScreenUtil().setHeight(17) / ScreenUtil().setSp(12)
                                          ),),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                                onTap: (){
                                  Navigator.of(context).push(CustomRoute(SchoolMarketDetail(id:markets[index]["mid"] , name:markets[index]["name"]))) ;
                                },
                              ) ;
                            }),
                          ) ,
                        ) ,
                        Container() 
                      ],
                    ),
                  ),
                )
              )
            ],
          ),
        ),
      ) ,
    ) ;
  }
}